/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes.ui;

import com.android1.amarena2d.nodes.sprites.BaseSprite;
import com.android1.amarena2d.texture.TextureFrame;

public class ToggleButton extends Button {

    public ToggleButton(BaseSprite entity) {
        super(entity);
    }

    protected TextureFrame selectedFrame;

    @Override
    protected void initListener() {
        this.buttonListenerAdapter = new ButtonListenerAdapter(this);
    }

    public void setStateSelected() {
        this.setState( State.Selected);
        if (selectedFrame == null)
            entity.setTextureFrame(pressedFrame);
        else
            entity.setTextureFrame(selectedFrame);
    }

    public boolean isStateSelected() {
        return state == State.Selected;
    }

    public TextureFrame getSelectedFrame() {
        return selectedFrame;
    }

    public void setSelectedFrame(TextureFrame selectedFrame) {
        this.selectedFrame = selectedFrame;
    }

    protected static final class ButtonListenerAdapter extends Button.ButtonListenerAdapter {

        ToggleButton button;
        boolean selected;

        public ButtonListenerAdapter(ToggleButton button) {
            super(button);
            this.button = button;
        }

        public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {

            if (!isHit)
                return false;

            if (buttonListener != null)
                buttonListener.onTapDown(button, x, y, pointer);

            selected = button.isStateSelected();

            if (button.isStateActive() || button.isStateSelected()) {
                button.setStatePressed();
            }

            hit = true;
            return true;
        }

        @Override
        public boolean onTouchUp(float x, float y, int pointer, boolean isHit) {
            if (isHit) {
                if (buttonListener != null) {
                    buttonListener.onTapUp(button, x, y, pointer);
                    if (hit && isHit) {
                        buttonListener.onTap(button, x, y, pointer);
                    }
                }
                if (hit) {
                    if (!selected)
                        button.setStateSelected();
                    else
                        button.setStateActive();
                }
                hit = false;
            } else if(hit) {
                if (selected)
                    button.setStateSelected();
                else
                    button.setStateActive();
            }
            hit = false;
            return isHit;
        }

        @Override
        public boolean onTouchDragged(float x, float y, int pointer, boolean isHit) {
            return false;
        }
    }


}